package com.tamecode.reflect;

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

/**
 * @author Liqc
 * @date 2021/8/3 17:59
 */
// 1.管理Bean生命周期的接口
public class Car implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean {

    private String brand;

    private String color;

    private int maxSpeed;

    private BeanFactory beanFactory;

    private String beanName;

    public Car() {
        System.out.println("调用car()构造器！");
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void setMaxSpeed(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

    public void setBrand(String brand) {
        System.out.println("调用setBrand()方法！");
        this.brand = brand;
    }

    public String getBrand() {
        return brand;
    }

    public String getColor() {
        return color;
    }

    public int getMaxSpeed() {
        return maxSpeed;
    }

    public Car(String brand, String color, int maxSpeed) {
        this.brand = brand;
        this.color = color;
        this.maxSpeed = maxSpeed;
    }

    public void introduce() {
        System.out.println("Car{" +
                "brand='" + brand + '\'' +
                ", color='" + color + '\'' +
                ", maxSpeed=" + maxSpeed +
                '}');
    }

    // 2.BeanFactoryAware接口方法
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("调用 BeanFactoryAware.setBeanFactory().");
        this.beanFactory = beanFactory;
    }

    // 3.BeanNameAware 接口方法
    public void setBeanName(String beanName) {
        System.out.println("调用 BeanNameAware.setBeanName()。");
        this.beanName = beanName;
    }

    // 4.InitializingBean 接口方法
    public void afterPropertiesSet() throws Exception {
        System.out.println("调用 InitializingBean.afterPropertiesSet()。");
    }

    //5.DisposableBean 接口方法
    public void destroy() throws Exception {
        System.out.println("调用 DisposableBean.destroy()。");
    }

    //6.通过<bean>的init-method属性指定初始化方法
    public void myInit() {
        System.out.println("调用 init-method 所指定的 myInit()，将 maxSpeed 设置为 240。");
        this.maxSpeed = 240;
    }

    //7.通过<bean>的destroy-method 属性指定销毁方法
    public void myDestroy() {
        System.out.println("调用 destroy-method 所指定的 myDestroy()。");
    }
}
